package com.chao.common.valid;


import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;

public class ChaoValueValidator implements ConstraintValidator<ListValue, Integer> {
//public class ChaoValueValidator implements ConstraintValidator<ListValue, String> {

//    private HashSet set = new HashSet<Double>();
    private HashSet set = new HashSet<Integer>();
    @Override
    public void initialize(ListValue constraintAnnotation) {

        int[] vals = constraintAnnotation.chao_Vals();
        System.out.println(vals.toString());
        //方式一：
        Arrays.asList(vals).forEach(e->set.add(e));
        //方式二：
//        Arrays.asList(vals).forEach(set ::add);//不能这么写 HashSet :: add

//        Arrays.asList(vals).forEach(System.out::println);

        System.out.println("---------------------------------");
        for (Object o : set) {
            System.out.println(o);
        }
        System.out.println("---------------------------------");

    }

//    @Override
//    public boolean isValid(String value, ConstraintValidatorContext context) {
//        System.out.println("set中值: "+set);
//        System.out.println("value = "+value);
//        return set.contains(value);
//    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        System.out.println("set中值: "+Arrays.asList(set).toString());
        for (Object o : set) {
            System.out.println(o);
            System.out.println((int)o);
        }

        System.out.println("========");
        System.out.println("value = "+value);
        return set.contains(value);
    }

//    public static void main(String[] args) {
//        HashSet<Integer> integers = new HashSet<>();
//        integers.add(1);
//        integers.add(2);
//        //1都是可以的
//        for (Object o : integers) {
//            System.out.println(o);
//        }
//        //2都是可以的
//        integers.forEach(i->{
//            System.out.println(i);
//        });
//        //3都是可以的
//        integers.forEach(System.out::print);
//    }


}
